
package com.lu.project.controller;
import cn.hutool.core.util.ObjectUtil;
import com.lu.project.common.BaseResponse;
import com.lu.project.common.PageResponse;
import com.lu.project.common.ResultUtils;
import com.lu.project.exception.ErrorCode;
import com.lu.project.exception.ThrowUtils;
import com.lu.project.model.dto.MemberElderDto.MemberElderDto;
import com.lu.project.model.vo.MemberElderVo;
import com.lu.project.service.MemberElderService;
import com.lu.project.utils.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户老人关联 Controller
 */
@RestController
@RequestMapping("/customer/memberElder")
@Api(tags = "客户老人关联")
public class MemberElderController {

    @Autowired
    private MemberElderService memberElderService;

    /**
     * 新增客户老人关联记录
     *
     * @param memberElderDto 客户老人关联 DTO
     * @return 操作结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增客户老人关联记录")
    public BaseResponse add(@RequestBody @Validated MemberElderDto memberElderDto) {
        check(memberElderDto);
        memberElderService.add(memberElderDto);
        return ResultUtils.success("新增成功");
    }

    /**
     * 根据id更新客户老人关联记录
     *
     * @param memberElderDto 客户老人关联 DTO
     * @return 操作结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "根据id更新客户老人关联记录")
    public BaseResponse update(@RequestBody @Validated MemberElderDto memberElderDto) {
        check(memberElderDto);
        memberElderService.update(memberElderDto);
        return ResultUtils.success("更新绑定关系成功");
    }
    public void check(MemberElderDto memberElderDto){
        Long elderId = memberElderDto.getElderId();
        String name = memberElderDto.getName();
        String phone = memberElderDto.getPhone();
        String relationship = memberElderDto.getRelationship();
        ThrowUtils.throwIf(UserThreadLocal.getUserId()==null, ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
        ThrowUtils.throwIf(ObjectUtil.hasEmpty(elderId,name,relationship),ErrorCode.PARAMS_ERROR,"绑定信息未填写完整");
    }
    /**
     * 根据id删除客户老人关联记录
     *
     * @param id 客户老人关联id
     * @return 操作结果
     */
    @DeleteMapping("/deleteById")
    @ApiOperation(value = "根据id删除客户老人关联记录")
    public BaseResponse deleteById(@RequestParam Long id) {
        memberElderService.deleteById(id);
        return ResultUtils.success("解除绑定成功");
    }

    /**
     * 根据id查询客户老人关联记录
     *
     * @param id 客户老人关联id
     * @return 客户老人关联实体类
     */
    @GetMapping("/getById")
    @ApiOperation(value = "根据id查询客户老人关联记录")
    public BaseResponse getById(@RequestParam Long id) {
        MemberElderVo memberElder = memberElderService.getById(id);
        return ResultUtils.success(memberElder,"获取绑定关系成功");
    }

    /**
     * 我的家人列表
     *
     * @return 客户老人关联实体类
     */
    @GetMapping("/my")
    @ApiOperation(value = "我的家人列表")
    public BaseResponse my() {
        List<MemberElderVo> memberElders = memberElderService.my();
        return ResultUtils.success(memberElders);
    }

    /**
     * 分页查询客户老人关联记录
     *
     * @param memberId 客户id
     * @param elderId  老人id
     * @param pageNum  当前页码
     * @param pageSize 每页记录数
     * @return 客户老人关联记录列表
     */
    @GetMapping("/list-by-page")
    @ApiOperation(value = "分页查询客户老人关联记录")
    public BaseResponse<PageResponse<MemberElderVo>> listByPage(Long memberId, Long elderId, Integer pageNum, Integer pageSize) {
        PageResponse<MemberElderVo> pageInfo = memberElderService.listByPage(memberId, elderId, pageNum, pageSize);
        return ResultUtils.success(pageInfo);
    }

}


